package com.yuerlive.rainbow.module.main.fragment;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.text.Layout;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.wuxiaolong.pullloadmorerecyclerview.PullLoadMoreRecyclerView;
import com.yuerlive.rainbow.R;
import com.yuerlive.rainbow.base.BaseFragment;
import com.yuerlive.rainbow.controller.LiveRoomController;
import com.yuerlive.rainbow.entity.LiveInfo;
import com.yuerlive.rainbow.entity.ObjectWrapper;
import com.yuerlive.rainbow.module.homepage.HomePageActivity;
import com.yuerlive.rainbow.module.live.LiveRoomActivity;
import com.yuerlive.rainbow.module.main.adapter.MainHotAdapter;
import com.yuerlive.rainbow.module.main.mainInterface.MainFragmentListener;
import com.yuerlive.rainbow.module.main.presenter.HomeContract;
import com.yuerlive.rainbow.module.main.presenter.HomePresenter;
import com.yuerlive.rainbow.widget.CustomDialog;
import com.yuerlive.rainbow.widget.LinearLayoutManagerWrapper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by xiaoyi on 2017/5/17.
 */

public class FragmentHot extends BaseFragment implements HomeContract.View {
    @BindView(R.id.refresh)
    PullLoadMoreRecyclerView refresh;
    MainHotAdapter adapter;
    List<ObjectWrapper> mDatas = new ArrayList<>();
    HomeContract.Presenter presenter;
    private static final int SCROLL_DISTANCE = 35;
    private int totalScrollDistance;
    private MainFragmentListener topListener;
    private MainFragmentListener buttomListener;
    private boolean isLoading = false;
    private boolean isRefreshing = false;


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_recycle;
    }

    @Override
    public void initViews(View root) {
        presenter = new HomePresenter(this);
        mDatas.clear();
        presenter.getData();
        refresh.setRefreshing(true);
        refresh.setColorSchemeResources(R.color.colorPrimary);
        LinearLayoutManagerWrapper linearLayoutManager = new LinearLayoutManagerWrapper(mActivity, LinearLayoutManager.VERTICAL, false);
        refresh.getRecyclerView().setLayoutManager(linearLayoutManager);
        refresh.setOnPullLoadMoreListener(new PullLoadMoreRecyclerView.PullLoadMoreListener() {
            @Override
            public void onRefresh() {
                isRefreshing = true;
                presenter.refresh();
            }

            @Override
            public void onLoadMore() {
                if (!isLoading) {
                    isLoading = true;
                    presenter.getHotLive();
                }
            }
        });
        refresh.getRecyclerView().setOnTouchListener((v, event) -> {
            if (isRefreshing) return false;

            switch (event.getAction()) {
                case MotionEvent.ACTION_UP:

                    int topReal = ((FragmentMain) getParentFragment()).getTobarRealHeight();
                    int topTransY = ((FragmentMain) getParentFragment()).getTopTranslationY();
                    if (Math.abs(topTransY) >= topReal / 2) {
                        refresh.getRecyclerView().smoothScrollBy(0, topReal - Math.abs(topTransY));
                        ((FragmentMain) getParentFragment()).doTopTranslate(topReal);
                        return false;
                    } else if (Math.abs(topTransY) < topReal / 2) {
                        refresh.getRecyclerView().smoothScrollBy(0, topTransY);
                        ((FragmentMain) getParentFragment()).doTopTranslate(0);
                        return false;
                    }
                    break;
            }
            return false;
        });
        adapter = new MainHotAdapter(mActivity, mDatas);
        adapter.setOnItemClickListener((v, p) -> {
            //临时解决mDatas数据为空的时候点击item
            if (mDatas.size() > 0) {
                LiveInfo info = (LiveInfo) mDatas.get(p).object;
                if (info.getState() != 0) {
                    Intent i = new Intent();
                    i.setClass(mActivity, LiveRoomActivity.class);
                    i.putExtra("liveId", info.getLiveId());
                    i.putExtra("chatroomId", info.getChatRoomId());
                    i.putExtra("liveCover", info.getLiveIcon());
                    LiveRoomController.getInstance().enterLiveRoom(mActivity, i);
                } else {
                    Intent i = new Intent();
                    i.setClass(mActivity, HomePageActivity.class);
                    i.putExtra("otherId", info.getUserId());
                    jumpToActivity(i);
                }
            }
        });
        refresh.setAdapter(adapter);

    }


    @Override
    public void setPresenter(HomeContract.Presenter presenter) {
        super.presenter = presenter;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void hideLoading() {
        isLoading = false;
        refresh.setPullLoadMoreCompleted();
        isRefreshing = false;
    }

    @Override
    public void showFullData(List<ObjectWrapper> results) {
        mDatas.clear();
        mDatas.add(new ObjectWrapper(MainHotAdapter.TYPE_HEADER, null));
        mDatas.addAll(results);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void showLiveData(List<ObjectWrapper> results, int page) {
    }

    @Override
    public void showMoreData(List<ObjectWrapper> results) {
        if (results != null && results.size() != 0) {
            mDatas.addAll(results);
            adapter.notifyDataSetChanged();
        } else {
            refresh.setRefreshing(false);
//            ToastUtils.show("没有更多数据");
        }
        isRefreshing = false;
        refresh.setPullLoadMoreCompleted();
    }

    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof MainFragmentListener)
            buttomListener = (MainFragmentListener) context;
    }
}
